﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TSSD0001 :SDBASE
    {
        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
            typeof(string), typeof(TSSD0001),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return ((VSDDAT)MV).KEY;
            }
            set
            {
                SetValue(KEYProperty, value);
                ((VSDDAT)MV).KEY = value;
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSSD0001;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion
        #region ==SDSA===========
        public static readonly DependencyProperty SDSAProperty = DependencyProperty.Register("SDSA",
            typeof(string), typeof(TSSD0001),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSDSAChanged));
        public string SDSA
        {
            get
            {
                return ((VSDDAT)MV).SDSA;
            }
            set
            {
                SetValue(SDSAProperty, value);
                ((VSDDAT)MV).SDSA = value;
            }
        }
        private static void OnSDSAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSSD0001;
            if (src != null)
            {
                src.SDSA = (string)e.NewValue;
            }
        }
        #endregion
        #region ==dwKEY===========
        public static readonly DependencyProperty dwKEYProperty = DependencyProperty.Register("dwKEY",
            typeof(DataRow), typeof(TSSD0001),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwKEYChanged));
        public DataRow dwKEY
        {
            get
            {
                return ((VSDDAT)MV).dwKEY;
            }
            set
            {
                SetValue(dwKEYProperty, value);
                ((VSDDAT)MV).dwKEY = value;
                if (value != null)
                {
                    KEY = value["KEY"].ToString();
                }
            }
        }
        private static void OndwKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSSD0001;
            if (src != null)
            {
                src.dwKEY = (DataRow)e.NewValue;
            }
        }
        #endregion
        #region ==VALUE===========
        public static readonly DependencyProperty VALUEProperty = DependencyProperty.Register("VALUE",
            typeof(string), typeof(TSSD0001),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVALUEChanged));
        public string VALUE
        {
            get
            {
                return ((VSDDAT)MV).VALUE;
            }
            set
            {
                SetValue(VALUEProperty, value);
                ((VSDDAT)MV).VALUE = value;
            }
        }
        private static void OnVALUEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSSD0001;
            if (src != null)
            {
                src.VALUE = (string)e.NewValue;
            }
        }
        #endregion
     
        public TSSD0001()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VSDDAT();
            DataContext = MV;
            SDSA = "0001"; 
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwKEY = dwMaster;
        } 
  
        public override void LOADALL()
        {
            if (SDSA.Trim().Length == 0)
                return;
            dwData = TBMain.NewRow();
            dwData["SDSA"] = SDSA;
             //dwData["SDKEY"] = "00" + KEY.Trim();
            SResult = ((VSDDAT)MV).QuerySDDAT(dwData, Convert.ToInt16(SDSA));  
            MergeData(SResult,false); 
        }
        public void Locate()
        {
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["KEY"].ToString().Trim() == KEY.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                int idx=TBMain.Rows.IndexOf(row);
                vwMaster.View.MoveCurrentToPosition(idx);
                break;
            }
            
        }

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                LOADALL();
            }
            else
            {
                VICC_WFEVENTS.SVWF.SD02(RUNID, SDSA);
            }
            base.ManageCommand(sender, e);
        }
    }
}
